package com.example.woke;

import android.app.Activity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.TextView;
import android.widget.Toast;

import com.http.HttpMethods;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.JsonHttpResponseHandler;
import com.loopj.android.http.PersistentCookieStore;
import com.loopj.android.http.RequestParams;
import com.woke.adapter.Myclearadapter;
import com.woke.data.Datas_clear;
import com.woke.diyview.XListView;
import com.woke.diyview.XListView.IXListViewListener;
import com.woke.method.MyApp;
import com.zhongcai.online.R;

import org.apache.http.Header;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;

public class MywalletclearActivity extends Activity implements OnClickListener ,OnItemClickListener, IXListViewListener{

	private TextView mTmoney, mTmoney2;
	private TextView mTtixian,mTtixian2;
	private MyApp myApp;
	private String wxamount;
	private String zfbamount;
	private XListView mListView;
	private ArrayList<Datas_clear> datalist = new ArrayList<Datas_clear>();
	private LayoutInflater inflater;
	private Myclearadapter adapter;
	private int pageno = 1;
	private String  wayPay ;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_mywalletclear);
		inflater=getLayoutInflater();
		intview();

	}

	private void intview() {
		findViewById(R.id.avmywalletonlive_image_back).setOnClickListener(this);
		mTmoney = (TextView) findViewById(R.id.avmywa_claer_money);
		mTmoney2 = (TextView) findViewById(R.id.avmywa_clear_money2);
		mTtixian = (TextView) findViewById(R.id.avmywa_clear_tixian);
		mTtixian.setOnClickListener(this);
		mTtixian2 = (TextView) findViewById(R.id.avmywa_clear_tixian2);
		mTtixian2.setOnClickListener(this);
		myApp = (MyApp) getApplication();
		mListView = (XListView) findViewById(R.id.userinfo_clear_listview);
		mListView.setPullLoadEnable(true);
		mListView.setXListViewListener(this);
		mListView.setOnItemClickListener(this);
		adapter = new Myclearadapter(inflater,datalist);
		mListView.setAdapter(adapter);
		if(myApp.getDatas_load ()!=null) {
			String userId = myApp.getDatas_load ().getId ();
			getMoney (userId);
			getdata ();
		}
	}

	@Override
	public void onClick(View v) {
		String money = mTmoney.getText().toString();
		String money2 = mTmoney2.getText().toString();
		switch (v.getId()) {
		case R.id.avmywalletonlive_image_back:
			finish();
			break;

		case R.id.avmywa_clear_tixian://微信提款
			wayPay = "1";
			gettixian(wayPay);
			break;
		case R.id.avmywa_clear_tixian2://支付宝提款
			wayPay = "2";
			gettixian(wayPay);
			break;

		default:
			break;
		}
	}

	// 获取微信，支付宝待清金额
	private void getMoney(final String userid) {
		AsyncHttpClient lient = new AsyncHttpClient();
		String url = HttpMethods.BASEURL + "g=api&m=Scancode&a=getAmount&userId="
				+ userid;
		// http://app.88china.com:8384/index.php?g=api&m=Scancode&a=getAmount&userId=1391
		lient.get(url, new JsonHttpResponseHandler() {
			@Override
			public void onSuccess(int statusCode, Header[] headers,
					JSONObject response) {
				// TODO Auto-generated method stub
				super.onSuccess(statusCode, headers, response);
				try {
					String flag = response.getString("flag");
					if (flag.equals("success")) {
						JSONObject jsonObject = response.getJSONObject("data");
						JSONObject json = jsonObject.getJSONObject("data");
						wxamount = json.getString("wxamount");
						zfbamount = json.getString("zfbamount");

						mTmoney.setText("￥"+wxamount);
						mTmoney2.setText("￥"+zfbamount);
					}
				} catch (JSONException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}

			}

			@Override
			public void onFailure(int statusCode, Header[] headers,
					Throwable throwable, JSONObject errorResponse) {
				// TODO Auto-generated method stub
				super.onFailure(statusCode, headers, throwable, errorResponse);
				Toast.makeText(MywalletclearActivity.this, "数据失败！",
						Toast.LENGTH_SHORT).show();
			}
		});

	}
	private void getdata() {
		if(myApp.getDatas_load ()==null)return;
		AsyncHttpClient ahc = new AsyncHttpClient();
		RequestParams params = new RequestParams();
		PersistentCookieStore myCookieStore = new PersistentCookieStore(MywalletclearActivity.this);
		ahc.setCookieStore(myCookieStore);
//		http://app.88china.com:8384/index.php?g=api&m=Scancode&a=getPayLog&userId=1391
		params.put("g", "api");
		params.put("m", "Scancode");
		params.put("a", "getPayLog");
		params.put("userId", myApp.getDatas_load().getId());
		params.put("page", pageno);
		ahc.get(HttpMethods.BASEURL, params, new JsonHttpResponseHandler(){
			@Override
			public void onSuccess(int statusCode, Header[] headers,
					JSONObject response) {
				super.onSuccess(statusCode, headers, response);
				System.err.println("page------------------"+pageno);
				try {
					String flag = response.getString("flag");
					if(flag.equals("success")){
						
						JSONObject jsonObject = response.getJSONObject("data");
						JSONObject jsonObject2 = jsonObject.getJSONObject("data");
						String json = jsonObject2.getString("list");
						
						System.out.println("json------------------"+json);
						ArrayList<Datas_clear> parseArray = (ArrayList<Datas_clear>) com.alibaba.fastjson.JSONArray.parseArray(json, Datas_clear.class);

						System.out.println("datalist--------------"+parseArray);
						datalist.addAll(parseArray);
						adapter.notifyDataSetChanged();
						pageno=pageno+1;
						System.out.println("pageno"+pageno);
					}
				} catch (JSONException e) {
					e.printStackTrace();
				}
			}
			
			@Override
			public void onFailure(int statusCode, Header[] headers,
					String responseString, Throwable throwable) {
				super.onFailure(statusCode, headers, responseString, throwable);
			}
			@Override
			public void onStart() {
				super.onStart();
			}
			@Override
			public void onFinish() {
				super.onFinish();
			}
		});
		
	}
    

	private void gettixian(String payWay) {
		if(myApp.getDatas_load ()==null)return;
		AsyncHttpClient ahc = new AsyncHttpClient();
		RequestParams params = new RequestParams();
		PersistentCookieStore myCookieStore = new PersistentCookieStore(MywalletclearActivity.this);
		ahc.setCookieStore(myCookieStore);
//		http://app.88china.com:8384/index.php?g=api&m=Scancode&a=drawAmount&userId=1391&payWay=1
		params.put("g", "api");
		params.put("m", "Scancode");
		params.put("a", "drawAmount");
		params.put("userId", myApp.getDatas_load().getId());
		params.put("payWay", payWay);
		ahc.get(HttpMethods.BASEURL, params, new JsonHttpResponseHandler(){
			@Override
			public void onSuccess(int statusCode, Header[] headers,
					JSONObject response) {
				super.onSuccess(statusCode, headers, response);
				System.err.println("page------------------"+response);
				
				try {
					String info = response.getString("info");
					Toast.makeText(MywalletclearActivity.this, info, Toast.LENGTH_SHORT).show();
				} catch (JSONException e) {
					e.printStackTrace();
				}
			}
			
			@Override
			public void onFailure(int statusCode, Header[] headers,
					String responseString, Throwable throwable) {
				super.onFailure(statusCode, headers, responseString, throwable);
			}
			@Override
			public void onStart() {
				super.onStart();
			}
			@Override
			public void onFinish() {
				super.onFinish();
			}
		});
	}

	@Override
	public void onRefresh() {
		// TODO Auto-generated method stub
		pageno = 1;
		getdata();
	}

	@Override
	public void onLoadMore() {
		// TODO Auto-generated method stub
		getdata();
	}

	@Override
	public void onItemClick(AdapterView<?> adapterview, View view, int i, long l) {
		// TODO Auto-generated method stub
		
	}
	
}
